import java.util.Scanner;

public class TestDemo02 {
    //创建图书馆
    static Library library = new Library("新华书店");
    static User loginUser;
    static Scanner scanner;

    public static void main(String[] args) {
        System.out.println("欢迎来到新华书店");
        init();// 初始化数据
        while (true) {
            System.out.println("1、登陆");
            System.out.println("2、借书");
            System.out.println("3、支付");
            System.out.println("4、还书");
            System.out.println("5、退出登陆");
            System.out.println("6、打印图书");
            System.out.println("7、退出系统");
            System.out.println("8、借书列表");
            scanner = new Scanner(System.in);
            int i = scanner.nextInt();
            if (i == 1) {
                login();
            } else if (i == 2) {
                borrowBook();
            } else if (i == 3) {
                pay();
            } else if (i == 4) {
                returnBook();
            } else if (i == 5) {
                logout();
            } else if (i == 6) {
                printBooks();
            } else if (i == 7) {
                break;
            } else if (i == 8) {
                printBorrowBooks();
            }
        }

    }

    private static void printBorrowBooks() {
        for (BorrowBook book : library.getBorrowBooks()) {
            if (book != null) {
                System.out.println("名字：" + book.getName());
                System.out.println("数量：" + book.getCount());
                System.out.println("天数：" + book.getDay());
                System.out.println("借书人：" + book.getUser().getName());
                System.out.println("-----------------------------------------");
            }

        }
    }

    private static void printBooks() {
        for (Book book : library.getBooks()) {
            if (book != null) {
                System.out.println("名字：" + book.getName());
                System.out.println("数量：" + book.getStock());
                System.out.println("价格：" + book.getPrice());
                System.out.println("作者：" + book.getAuthor());
                System.out.println("-----------------------------------------");
            }

        }

    }

    private static void logout() {
        library.clearBorrowBooks();
        loginUser = null;
        System.out.println("退出成功");
    }

    private static void init() {
        for (int i = 0; i < 10; i++) {
            // 创建除10本书
            Book book = new Book("西游记" + i, "吴承恩", 10, 7);
            // 把书放到图书馆中
            library.addBook(book);
        }

        // 用户
        User zs = new User("张三", "zs", "123456", 100f);
        User ls = new User("李四", "ls", "123456", 100f);
        library.addUser(zs);
        library.addUser(ls);
    }

    private static void login() {
        if (loginUser == null) {
            System.out.println("请输入账号");
            String account = scanner.next();
            System.out.println("请输入密码");
            String password = scanner.next();
            loginUser = library.login(account, password);
            if (loginUser != null) {
                System.out.println("登陆成功");
            } else {
                System.out.println("登陆失败");
            }

        } else {
            System.out.println("请先退出");
        }

    }

    private static void borrowBook() {
        if (loginUser != null) {
            System.out.println("请输入要借的书名");
            String name = scanner.next();
            System.out.println("请输入要借的数量");
            int count = scanner.nextInt();
            System.out.println("请输入要借的天数");
            int day = scanner.nextInt();
            // 借书
            boolean result = library.borrowBook(name, count, day, loginUser);//借书
            System.out.println(result ? "成功" : "失败");
        } else {
            System.out.println("请先登陆");
        }

    }

    private static void pay() {
        if (loginUser != null) {
            // 支付
            boolean res = library.pay(loginUser);
            System.out.println(res ? "支付成功" : "支付失败");
            System.out.println("余额还剩" + loginUser.getBalance());
        } else {
            System.out.println("请先登陆");
        }
    }

    private static void returnBook() {
        System.out.println("请输入要还的书名");
        String name = scanner.next();
        System.out.println("请输入要还的数量");
        int count = scanner.nextInt();
        // 还
        boolean result = library.returnBook(name, count);//还书
        System.out.println(result ? "成功" : "失败");
    }


}
